package com.lee.pullrefresh.ui;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;

/**
 * Created by Administrator on 2017/1/18 0018.
 */

public class PullToRefreshRecyclerView extends PullToRefreshBase<RecyclerView> {

    private RecyclerView mRecyclerView;
    /**
     * 加载更多 footer
     */
    private LoadingLayout mLoadMoreFooterLayout;

    public PullToRefreshRecyclerView(Context context) {
        this(context, null);
    }

    public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullToRefreshRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        setPullLoadEnabled(false);
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        mRecyclerView = new RecyclerView(context);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (isScrollLoadEnabled() && hasMoreData()) {
                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE
                            || newState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                        if (isReadyForPullUp()) {
                            startLoading();
                        }
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mRecyclerView.setLayoutManager(
                new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

        return mRecyclerView;
    }

    @Override
    public void setScrollLoadEnabled(boolean scrollLoadEnabled) {
        super.setScrollLoadEnabled(scrollLoadEnabled);
        if (scrollLoadEnabled) {
            if (null == mLoadMoreFooterLayout)
                mLoadMoreFooterLayout = new FooterLoadingLayout(getContext());
            if (null == mLoadMoreFooterLayout.getParent()) {
//                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
//                        ViewGroup.LayoutParams.WRAP_CONTENT);
//                params.gravity = Gravity.BOTTOM;
//                mRecyclerView.getLayoutManager().addView(mLoadMoreFooterLayout,1);
            }
            mLoadMoreFooterLayout.show(true);

        } else {
            if (null != mLoadMoreFooterLayout)
                mLoadMoreFooterLayout.show(false);
        }
    }

    /**
     * 表示是否还有更多数据
     *
     * @return true表示还有更多数据
     */
    private boolean hasMoreData() {
        if ((null != mLoadMoreFooterLayout) && (mLoadMoreFooterLayout.getState() == ILoadingLayout.State.NO_MORE_DATA)) {
            return false;
        }
        return true;
    }

    //
    @Override
    protected LoadingLayout createHeaderLoadingLayout(Context context, AttributeSet attrs) {
        return new RotateLoadingLayout(context);
    }

    public void setHasMoreData(boolean hasMoreData) {
        if (!hasMoreData) {
            if (null != mLoadMoreFooterLayout) {
                mLoadMoreFooterLayout.setState(ILoadingLayout.State.NO_MORE_DATA);
            }

            LoadingLayout footLayoutingLayout = getFooterLoadingLayout();
            if (null != footLayoutingLayout) {
                footLayoutingLayout.setState(ILoadingLayout.State.NO_MORE_DATA);
            }
        }
    }

    @Override
    protected void startLoading() {
        super.startLoading();
        if (null != mLoadMoreFooterLayout) {
            mLoadMoreFooterLayout.setState(ILoadingLayout.State.REFRESHING);
        }
    }

    @Override
    public void onPullUpRefreshComplete() {
        super.onPullUpRefreshComplete();
        if (null != mLoadMoreFooterLayout) {
            mLoadMoreFooterLayout.setState(ILoadingLayout.State.RESET);
        }

    }

    @Override
    public LoadingLayout getFooterLoadingLayout() {
        return super.getFooterLoadingLayout();
    }

    @Override
    protected boolean isReadyForPullDown() {
        return isFirstItemVisible();
    }

    @Override
    protected boolean isReadyForPullUp() {
        return isLastItemVisible();
    }


    public boolean isFirstItemVisible() {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
//        Log.e("Position","child :"+linearLayoutManager.getChildCount() + "top :"+linearLayoutManager.getChildAt(0).getTop());
        if (null != linearLayoutManager) return true;

        int mostTop = (linearLayoutManager.getChildCount() > 0) ? linearLayoutManager.getChildAt(0).getTop() : 0;
        Log.e("Position", "child :" + linearLayoutManager.getChildCount() + "top :" + linearLayoutManager.getChildAt(0).getTop());
        if (mostTop > 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断最后一个item是否 显示
     *
     * @return
     */
    public boolean isLastItemVisible() {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();

        if (null == linearLayoutManager) return true;

        final int lastItemPosition = linearLayoutManager.getItemCount() - 1;
        final int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
        if (lastVisiblePosition >= lastItemPosition - 1) {
            final int childIndex = lastVisiblePosition - linearLayoutManager.findFirstVisibleItemPosition();
            final int childCount = linearLayoutManager.getChildCount();
            final int index = Math.min(childCount - 1, childIndex);
            final View lastVisibleChild = linearLayoutManager.getChildAt(index);
            if (null != lastVisibleChild) {
                return lastVisibleChild.getBottom() <= mRecyclerView.getBottom();
            }
        }
        return false;
    }
}
